package com.example.broadcastlistener;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * @author hdz
 * @date 2019/06/22
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private FragmentManager mFragmentManager;
    private MessageListFragment mMsgListFragment;
    private AddListenerFragment mAddListenerFragment;
    private MenuItem mMenuItem = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getSupportFragmentManager();
        mMsgListFragment = new MessageListFragment();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.main_fragment, mMsgListFragment);
        fragmentTransaction.commit();

        // 取得ActionBar对象
        ActionBar actionBar =getSupportActionBar();
        if (actionBar != null) {
            //actionBar.setDisplayHomeAsUpEnabled(true);

            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            back();
            return true;
        } else if (item.getItemId() == R.id.menu_add) {
            showAddFragment();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
        if (menu.size() == 1) {
            mMenuItem = menu.getItem(0);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void showAddFragment() {
        if (mAddListenerFragment == null) {
            mAddListenerFragment = new AddListenerFragment();
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.hide(mMsgListFragment);
        if (mAddListenerFragment.isAdded()) {
            fragmentTransaction.show(mAddListenerFragment);
        } else {
            fragmentTransaction.add(R.id.main_fragment, mAddListenerFragment);
        }
        fragmentTransaction.commit();
        mMenuItem.setVisible(false);
    }

    private void back() {
        if (mAddListenerFragment!=null && mAddListenerFragment.isVisible()) {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.hide(mAddListenerFragment);
            fragmentTransaction.show(mMsgListFragment);
            fragmentTransaction.commit();
            mMenuItem.setVisible(true);
        } else {
            onBackPressed();
        }
    }
}
